package com.ocom.payconfig.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.card.EntityXfPointInfo;
import com.ocom.common.entity.payconfig.EntityMealPlan;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.request.payconfig.MealPlanListRequest;
import com.ocom.common.request.payconfig.RemoveMealPlanRequest;
import com.ocom.common.utils.CommonUtil;
import com.ocom.common.utils.DateUtil;
import com.ocom.common.vo.payconfig.OrderPlanMealVo;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoMealPlan;
import com.ocom.payconfig.service.MealPlanService;
import com.ocom.payconfig.service.XfMealTimeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class MealPlanServiceImpl extends ServiceImpl<IDaoMealPlan, EntityMealPlan> implements MealPlanService {

    @Autowired
    private IDaoMealPlan iDaoMealPlan;
    @Autowired
    private XfMealTimeService xfMealTimeService;

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB)
    public List<EntityMealPlan> mealPlanList(Long companyId, MealPlanListRequest request) {
        if (CommonUtil.isNull(request.getBeginDate())) {
            request.setBeginDate(DateUtil.getDate());
        }
        if (CommonUtil.isNull(request.getEndDate())) {
            request.setEndDate("2099-12-31");
        }
        return iDaoMealPlan.mealPlanList(companyId,
                request.getMerchId(),
                request.getEndDate(),
                request.getBeginDate(),
                request.getPointId());
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB)
    public Boolean removeMealPlan(Long companyId, RemoveMealPlanRequest request) {
        QueryWrapper<EntityMealPlan> queryWrapper = Wrappers.query();
        //com_id、merch_id、mealplan_id
        queryWrapper.eq("com_id", request.getComId());
//        queryWrapper.eq("merch_id", request.getMerchId());
        queryWrapper.eq("mealplan_id", request.getMealplanId());
        return this.remove(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB)
    public Boolean newMealPlan(Long companyId, EntityMealPlan entityMealPlan) {
        return this.save(entityMealPlan);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB)
    public Boolean updateMealPlan(Long companyId, EntityMealPlan entityMealPlan) {
        UpdateWrapper<EntityMealPlan> updateWrapper = Wrappers.update();
        updateWrapper.eq("mealplan_id", entityMealPlan.getMealplanId());
        updateWrapper.eq("merch_id", entityMealPlan.getMerchId());
        updateWrapper.eq("com_id", entityMealPlan.getComId());

        if (!CommonUtil.isNull(entityMealPlan.getPlanName()))
            updateWrapper.set("plan_name", entityMealPlan.getPlanName());
        if (entityMealPlan.getStartDate() != null) updateWrapper.set("start_date", entityMealPlan.getStartDate());
        if (entityMealPlan.getStopDate() != null) updateWrapper.set("stop_date", entityMealPlan.getStopDate());
        if (entityMealPlan.getRemark() != null) updateWrapper.set("remark", entityMealPlan.getRemark());
        if (!CommonUtil.isNull(entityMealPlan.getRemark())) updateWrapper.set("remark", entityMealPlan.getRemark());
        if (entityMealPlan.getCycleDays() != null) updateWrapper.set("cycle_days", entityMealPlan.getCycleDays());
        if (entityMealPlan.getReceiptType() != null) updateWrapper.set("receipt_type", entityMealPlan.getReceiptType());
        if (StringUtils.isNotBlank(entityMealPlan.getReceiptIds()))
            updateWrapper.set("receipt_ids", entityMealPlan.getReceiptIds());
        if (StringUtils.isNotBlank(entityMealPlan.getPlanType()))
            updateWrapper.set("plan_type", entityMealPlan.getPlanType());
        if (entityMealPlan.getConsType() != null) updateWrapper.set("cons_type", entityMealPlan.getConsType());

        return this.update(updateWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB)
    public Integer checkPlanName(Long companyId, EntityMealPlan entityMealPlan, boolean isNew) {
        QueryWrapper<EntityMealPlan> queryWrapper = Wrappers.query();
        queryWrapper.eq("com_id", entityMealPlan.getComId());
        queryWrapper.eq("merch_id", entityMealPlan.getMerchId());
        queryWrapper.eq("plan_name", entityMealPlan.getPlanName());
        if (!isNew && entityMealPlan.getMealplanId() != null) {
            queryWrapper.ne("mealplan_id", entityMealPlan.getMealplanId());
        }
        queryWrapper.last("limit 1");
        return this.count(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB)
    public List<EntityMealPlan> mealPlanList(Long companyId) {
        QueryWrapper<EntityMealPlan> queryWrapper = Wrappers.query();
        queryWrapper.eq("com_id", companyId);
        return this.list(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB)
    public EntityMealPlan mealPlanInfo(Long companyId, Integer mealplanId) {
        return this.getById(mealplanId);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB)
    public EntityMealPlan upToDateMealPlanInfo(Long companyId, Integer pointId, Integer receiptId, String mealDate) {
        QueryWrapper<EntityMealPlan> queryWrapper = Wrappers.query();
        queryWrapper.select("mealplan_id", "com_id", "plan_name", "MOD(DATEDIFF(\"" + mealDate + "\",start_date) ,cycle_days)+1 as days","merch_id","point_id");
        queryWrapper.eq("com_id", companyId);
//		queryWrapper.eq("merch_id", merchId);
        queryWrapper.eq(ObjectUtil.isNotEmpty(pointId) && pointId > 0,"point_id", pointId);
        if (null != receiptId && 0 < receiptId) {
            queryWrapper.apply(" ( receipt_Ids IS  NULL OR  find_in_set( " + receiptId + ", receipt_Ids ))");
        }
//        queryWrapper.between(mealDate,"start_date", "stop_date");
        queryWrapper.le("start_date", mealDate);
        queryWrapper.ge("stop_date", mealDate);
        queryWrapper.orderByDesc("update_time");
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB)
    public List<EntityMealPlan> getMerchPointList(Long companyId, Integer receiptId, String mealDate) {
//		QueryWrapper<EntityMealPlan> queryWrapper = Wrappers.query();
//		queryWrapper.select("merch_id","point_id");
//		queryWrapper.eq("com_id", companyId);
//		queryWrapper.like("receipt_ids", ","+receiptId+",");
//		queryWrapper.le("start_date", mealDate);
//		queryWrapper.ge("stop_date", mealDate);
//		queryWrapper.orderByAsc("merch_id","point_id");
//		return  this.list(queryWrapper);
        return iDaoMealPlan.getMerchPointList(companyId, receiptId, mealDate);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB)
    public List<OrderPlanMealVo> getOrderPlanMeal(Long companyId, String mealDate,String tagIds) {
        List<String> tagIdList = new ArrayList<>();
        if (!CommonUtil.isNull(tagIds)) {
            tagIdList = Arrays.asList(tagIds.split(","));
        }

        return iDaoMealPlan.getOrderPlanMeal(companyId, mealDate, tagIdList,null,0);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB)
    public List<OrderPlanMealVo> getOrderPlanMeal(Long companyId, String mealDate,String tagIds,Integer pointId) {
        List<String> tagIdList = new ArrayList<>();
        if (!CommonUtil.isNull(tagIds)) {
            tagIdList = Arrays.asList(tagIds.split(","));
        }
        Integer mealTimeCount = this.xfMealTimeService.getCount(companyId,pointId);
        return iDaoMealPlan.getOrderPlanMeal(companyId, mealDate, tagIdList,pointId,mealTimeCount);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB)
    public List<EntityXfPointInfo> getMealPlanPointList(Long companyId, Integer merchId) {
        String start =DateUtil.getDate();
        String end = DateUtil.getStrDate(DateUtil.getDateAfter(new Date(),15),"yyyy-MM-dd");
        return iDaoMealPlan.getMealPlanPointList(companyId, merchId,start,end);
    }
}
